package co.sanduche.teach.gallery;

import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;
import co.sanduche.teach.R;
import co.sanduche.teach.service.TaskExecutorService;
import co.sanduche.teach.service.TasksDBHelper;
import co.sanduche.teach.service.UpdatePhotoTask;
import co.sanduche.util.EPBaseActivity;

public class PhotoActivity extends EPBaseActivity {

	public PhotoActivity() {
		super("Kiido - Imagen");
	}

	private static final String FULLSCREEN_KEY = "fullscreen_key";
	protected static final String CURRENT_IMAGE_KEY = "current_image_key";
	static final int PHOTO_SIZE = 400;
	static final int FULLSCREEN_TRANSITION_TIME = 150;

	private GalleryDBOpenHelper dbHelper = new GalleryDBOpenHelper(
			PhotoActivity.this);
	private PhotoPageAdapter photoPageAdapter;
	private List<ImageItem> images;
	private Fragment currentFragment;
	private ActionBar actionBar;
	private DisplayMetrics displayMetrics;

	@SuppressLint("UseSparseArrays")
	protected HashMap<Integer, PhotoFragment> fragments = new HashMap<Integer, PhotoFragment>();
	boolean fullscreen = false;
	private ViewPager pager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			setContentView(R.layout.activity_photo);
			actionBar = getActionBar();
			actionBar.setDisplayHomeAsUpEnabled(true);
			Integer current = (Integer) getIntent().getExtras().get(
					CURRENT_IMAGE_KEY);
			photoPageAdapter = new PhotoPageAdapter(getSupportFragmentManager());
			images = dbHelper.loadAllData();

			pager = (ViewPager) findViewById(R.id.photoPager);
			pager.setAdapter(photoPageAdapter);

			displayMetrics = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(
					getDisplaymetrics());

			if (savedInstanceState != null) {
				showFullscreen(savedInstanceState.getBoolean(FULLSCREEN_KEY));
				current = savedInstanceState.getInt(CURRENT_IMAGE_KEY);
			}

			pager.setCurrentItem(current);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// when orientation changes, this invocation somehow keeps all code
		// after from executing
		super.onCreate(savedInstanceState);
	}

	protected void setVisibilityAllFragments() {
		for (PhotoFragment imgF : fragments.values())
			imgF.setVisibility();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putBoolean(FULLSCREEN_KEY, fullscreen);
		outState.putInt(CURRENT_IMAGE_KEY, pager.getCurrentItem());
	}

	public void update(ImageItem ii) {
		dbHelper.update(ii);

		Log.i("Test1", "Acabo de guardar imagen: " + dbHelper.get(ii.getId()));
		new TasksDBHelper(this).insertTask(UpdatePhotoTask.class, ii.getId());

		Intent i = new Intent(this, TaskExecutorService.class);
		startService(i);
	}

	public void showFullscreen(boolean full) {
		fullscreen = full;
		if (fullscreen)
			actionBar.hide();
		else
			actionBar.show();
		setVisibilityAllFragments();
	}

	public boolean isFullscreen() {
		return fullscreen;
	}

	public Fragment getCurrentFragment() {
		return currentFragment;
	}

	public void setCurrentFragment(Fragment currentFragment) {
		this.currentFragment = currentFragment;
	}

	public DisplayMetrics getDisplaymetrics() {
		return displayMetrics;
	}

	public ImageItem get(int pos) {
		return images.get(pos);
	}

	public class PhotoPageAdapter extends FragmentStatePagerAdapter {

		public PhotoPageAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int pos) {
			// Log.i(getClass().getSimpleName(), "pide fragmento " + pos);
			PhotoFragment photoFragment = PhotoFragment.newInstance(
					images.get(pos), pos, PhotoActivity.this);
			return photoFragment;
		}

		@Override
		public Object instantiateItem(ViewGroup arg0, int pos) {
			PhotoFragment fragment = (PhotoFragment) super.instantiateItem(
					arg0, pos);
			fragments.put(pos, fragment);
			return fragment;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// Log.i(getClass().getSimpleName(), "destruye fragmento " +
			// position);
			fragments.remove(position);
			super.destroyItem(container, position, object);
		}

		@Override
		public int getCount() {
			return images.size();
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return images.get(position).getPath();
		}

	}
}
